<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场地编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入场地编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基础价格" prop="basePrice">
        <el-input
          v-model="queryParams.basePrice"
          placeholder="请输入基础价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地状态" prop="status">
<!--        下拉框-->
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="可用" value="0"></el-option>
          <el-option label="正在使用" value="1"></el-option>
          <el-option label="正在维护" value="2"></el-option>
          <el-option label="关闭" value="3"></el-option>
        </el-select>
      </el-form-item>
<!--      场地类型-->
      <el-form-item label="场地类型" prop="type">
        <!--        下拉框-->
        <el-select v-model="queryParams.type" placeholder="请选择场地类型" clearable size="small">
          <el-option label="篮球场" value="0"></el-option>
          <el-option label="羽毛球场" value="1"></el-option>
          <el-option label="乒乓球场" value="2"></el-option>
          <el-option label="游泳场" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:place:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:place:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:place:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manager:place:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="placeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
<!--    展示场馆类型    0 篮球 1羽毛 2乒乓 3游泳-->
      <el-table-column label="场馆类型" align="center" prop="type" width="110">
        <template v-slot="scope">
          <el-tag v-if="scope.row.type === 0">篮球场</el-tag>
          <el-tag v-else-if="scope.row.type === 1">羽毛球场</el-tag>
          <el-tag v-else-if="scope.row.type === 2">乒乓球场</el-tag>
          <el-tag v-else-if="scope.row.type === 3">游泳场</el-tag>
          <el-tag v-else>其他</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="场地编号" align="center" prop="number" width="140" />

      <!--      场地图片-->
      <el-table-column label="场地图片" align="center" prop="avatar" width="90">
        <template v-slot="scope">
          <el-avatar shape="square" :size="50" :src="scope.row.image" />
        </template>
      </el-table-column>

      <el-table-column label="场地状态" align="center" prop="status" width="110">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0">可用</el-tag>
          <el-tag v-else-if="scope.row.status === 1">正在使用</el-tag>
          <el-tag v-else-if="scope.row.status === 2">正在维护</el-tag>
          <el-tag v-else-if="scope.row.status === 3">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="基础价格(半小时)" align="center" prop="basePrice" width="150">
        <template v-slot="scope">
          <el-tag v-if="scope.row.basePrice">{{ scope.row.basePrice/100 }}元</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="策略类型" align="center" prop="strategyId" width="120">
        <template v-slot="scope">
          <!--      策略类型  策略id strategyId  循环策略表 strategyList 根据策略id找到策略名字 name-->
          <el-tag v-for="item in strategyList" v-if="item.id === scope.row.strategyId">{{item.name}}</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="场地描述" align="center" prop="description" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:place:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:place:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场地管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="场地编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入场地编号" />
        </el-form-item>
        <!--        场馆类型 下拉框-->
        <el-form-item label="场馆类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择场馆类型">
            <!--  0 篮球 1羽毛 2乒乓 3游泳-->
            <el-option
              v-for="item in placeTypeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="基础价格" prop="basePrice">
<!--          基础价格单位为元  实际存储为分 需要乘以100-->
          <el-input v-model="form.basePrice" placeholder="请输入基础价格"></el-input>
        </el-form-item>


        <el-form-item label="场地状态" prop="status">
          <el-select v-model="form.status"  placeholder="请选择场地状态">
            <el-option label="可用" :value=0></el-option>
            <el-option label="正在使用" :value=1></el-option>
            <el-option label="正在维护" :value=2></el-option>
            <el-option label="关闭" :value=3></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="场地策略" prop="strategyId">
          <el-select v-model="form.strategyId" placeholder="请选择策略">
            <el-option
              v-for="item in strategyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="场地描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="场地图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlace, getPlace, delPlace, addPlace, updatePlace } from "@/api/manager/place"
import { listStrategy } from "@/api/manager/strategy"


export default {
  name: "Place",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场地管理表格数据
      placeList: [],
      //策略管理数据
      strategyList: [],
      // 场馆类型选项
      placeTypeOptions: [
        { id: 0, name: '篮球场' },
        { id: 1, name: '羽毛球场' },
        { id: 2, name: '乒乓球场' },
        { id: 3, name: '游泳场' }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        number: null,
        status: null,
        basePrice: null,
        description: null,
      },
      commonQueryParams:{
        pageNum: 1,
        pageSize: 10000
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "场地类型不能为空", trigger: "change" }
        ],
        number: [
          { required: true, message: "场地编号不能为空", trigger: "blur" }
        ],
        basePrice: [
          { required: true, message: "基础价格不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getPolicyList()
  },
  methods: {
    /** 查询场地管理列表 */
    getList() {
      this.loading = true
      listPlace(this.queryParams).then(response => {
        this.placeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /*获取策略列表*/
    getPolicyList(){
      this.loading = true
      listStrategy(this.commonQueryParams).then(response => {
        this.strategyList = response.rows
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        number: null,
        status: null,
        basePrice: null,
        description: null,
        strategyId: null,
        image: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加场地管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPlace(id).then(response => {
        /*修改返回数据的金额，除以100*/
        this.form = response.data
        this.form.basePrice /= 100

        this.title = "修改场地管理"
        this.open = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 创建临时对象用于提交，避免修改原始表单数据
          const submitData = { ...this.form };
          submitData.basePrice = submitData.basePrice * 100;

          if (this.form.id != null) {
            updatePlace(submitData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlace(submitData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除场地管理编号为"' + ids + '"的数据项？').then(function() {
        return delPlace(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manager/place/export', {
        ...this.queryParams
      }, `place_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
